 /**
 * @author caif
 * @fileoverview parse manifest file for data bag
 */
!function() {
    var root = this;

    var Databag = root.Databag = {};
    
    /**
	  * @description databag prototype initialize
     *  @param {String} data manifest json response data 
    */
    Databag.initialize = function(data) {
        this.Metadata = data.Metadata;
        this.Views = data.Views;
        this.Layouts = data.Layouts;
        this.jsonData = {};
        this.jsonData.data = [];
    }
    
    /**
	  * @description parse manifest json file
     *  @param {String} data manifest json response data 
    */
    Databag.parseManifest = function(data) {
        this.initialize(data);
        var layoutItem = this.getLayoutItemById(this.Metadata.DefaultLayout);
        for(var i = 0; i < layoutItem.Root.Folders.length; i++) {
            var dataItem = {};
            dataItem.metadata = {
                "ID": layoutItem.Root.Folders[i].ID,
                "Name": layoutItem.Root.Folders[i].Name,
                "root" : true,
                "children" : this.parseViews(layoutItem.Root.Folders[i].Views)
            };
            dataItem.data = layoutItem.Root.Folders[i].Name;
            dataItem.attr = {};
            if( i == 0 )
            {
                dataItem.attr["id"] = "initial_node";
            }
            
            dataItem.attr["rel"] = "folder";
            if( layoutItem.Root.Folders[i].Category != "Tab")
            {
            	dataItem.children = this.parseViews(layoutItem.Root.Folders[i].Views);
            	dataItem.metadata["type"]= "TreeNode";
            }
            else
            {
            	dataItem.metadata["type"] = "Tab";
            }
            dataItem.state = "open";
            this.jsonData.data.push(dataItem);
        }
    }
    
    /**
     *  @param {String} viwerIds array[] of viewer 
    */
    Databag.parseViews = function(viwerIds) {
        var viewers = [],
            dataItem = {};
        for(var i = 0; i < viwerIds.length; i++) {
            var dataItem = {},
                viewer = {};
            viewer = this.getViewerById(viwerIds[i]);
        	dataItem.data = {title : viewer.Name , Name : viewer.Name , ID : viewer.ID  };
            dataItem.attr = { "data-id" : viewer.ID , rel : "viewer"};
            dataItem.metadata = {
                "Representations": viewer.Representations
            }
            viewers.push(dataItem);
        }
        return viewers;
    }
    ,
    /**
     *  @param {String} viwerId viwerId
    */
    Databag.getViewerById = function(viwerId) {
        var length = this.Views.length;
        for(var i = 0; i < length; i++) {
            if(this.Views[i].ID == viwerId) {
                return this.Views[i];
                break;
            }
        }
    }
    ,
    /**
	  * @description get manifest layout
     *  @param {String} layoutId layoutId
    */
    Databag.getLayoutItemById = function(layoutId) {
        var length = this.Layouts.length;
        for(var i = 0; i < length; i++) {
            if(this.Layouts[i].ID == layoutId) {
                return this.Layouts[i];
                break;
            }
        }
    }
    ,
    
    /**
	  * @description get manifest rep
    *  @param {String} vid viewId
   */
   Databag.getRepByViewId = function(vid) {
       var length = this.Views.length;
       for(var i = 0; i < length; i++) {
           if(this.Views[i].ID == vid) {
               return this.Views[i].Representations;
               break;
           }
       }
   }
   
}()

